﻿Imports System.Text
Imports System.Web.HttpUtility

Public Class MailToLinkBuilder

	Public Property ToAddresses As New List(Of String)
	Public Property CCAddresses As New List(Of String)
	Public Property BCCAddresses As New List(Of String)
	Public Property Subject As String
	Public Property Body As String
	Public Property Attachment As IO.FileInfo

	'uri.escapeuristring

	Public Function GetMailToLink() As String
		Dim sb As New StringBuilder
		sb.Append("mailto:")

		Dim parameters As New List(Of String)

		If ToAddresses IsNot Nothing AndAlso ToAddresses.Count > 0 Then
			sb.Append(String.Join(";", ToAddresses.ConvertAll(Function(x) Uri.EscapeUriString(x))))
		End If

		If CCAddresses IsNot Nothing AndAlso CCAddresses.Count > 0 Then
			parameters.Add(String.Format("cc={0}", String.Join(";", CCAddresses.ConvertAll(Function(x) Uri.EscapeUriString(x)))))
		End If

		If BCCAddresses IsNot Nothing AndAlso BCCAddresses.Count > 0 Then
			parameters.Add(String.Format("bcc={0}", String.Join(";", BCCAddresses.ConvertAll(Function(x) Uri.EscapeUriString(x)))))
		End If

		If Subject <> "" Then
			parameters.Add(String.Format("subject={0}", Uri.EscapeUriString(Subject)))
		End If

		If Body <> "" Then
			parameters.Add(String.Format("body={0}", Uri.EscapeUriString(Body)))
		End If

		If Attachment IsNot Nothing AndAlso Attachment.Exists Then
			parameters.Add(String.Format("attachment={0}", Uri.EscapeUriString(Attachment.FullName)))
		End If

		If parameters.Count > 0 Then
			sb.Append("?")
			sb.Append(String.Join("&", parameters))
		End If

		Return sb.ToString
	End Function

End Class
